/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package UICommon;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.TableColumn;


/**
 *
 * @author localadmin
 */
public class Table
{
    public ResultSet _set=null;
    Vector<String> _columns=new Vector<String>();
    Vector _rows=new Vector();

    private static Table _instance=null;

    private Table()
    {
    }

    public static Table GetInstance()
    {
        if(_instance==null)
            _instance=new Table();
        return _instance;
    }

    public JTable GetTable()
    {
        JTable result=null;

        try
        {
            _set.beforeFirst();
            ResultSetMetaData meta=_set.getMetaData();
            int columncount=meta.getColumnCount();
            for(int i=1;i<=columncount;i++)
            {
                _columns.add(meta.getColumnName(i));
            }
            while(_set.next())
            {
                Vector row=new Vector();
                for(int i=1;i<=columncount;i++)
                {
                    row.add(_set.getObject(i));
                }
                _rows.add(row);
            }
            result=new JTable(_rows, _columns);
            TableColumn column;
            for (int i = 0; i < result.getColumnCount(); i++)
            {
                column = result.getColumnModel().getColumn(i);
                column.setMaxWidth(250);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                _set.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }


}
